var account = {
    initLoginForm:function(){
    	var options = {
            dataType : 'json',
            beforeSerialize : function(form, options) {
                options.url = 'account/login.json';
            },
            beforeSubmit : function() {
            	if(!$('#accountName').val()){
                	$('#loginErrorTips').html('帐号不能为空！');
                    $('#loginError').show();
                    return false;
                }
                if(!$('#pwd').val()){
                	$('#loginErrorTips').html('密码不能为空！');
                    $('#loginError').show();
                    return false;
                }
            	
                $('#signForm').attr('action', 'account/login.json');
                return true;
            },
            success : function(data) {
                if(data.result=='success'){
                    location.href = data.redirectUrl;
                }else{
                	$('#loginErrorTips').html('账号或密码错误！');
                    $('#loginError').show();
                }
            }
        };
        
        $('#signForm').submit(function() {
            $(this).ajaxSubmit(options);
            return false;
        });
    },
    initRegisterForm:function(){
        var options = {
            dataType : 'json',
            beforeSerialize : function(form, options) {
                options.url = 'account/register.json';
            },
            beforeSubmit : function() {
                if(!account.checkRegisterUserName()){
                    return false;
                }
                if(!account.checkRegisterPassword()){
                    return false;
                }
                if(!account.checkRegisterConfirmPassword()){
                    return false;
                }
                if(account.checkUserNameOnly()=='false'){
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>该用户名已被注册，请重新输入!</span>",
                        icon:"succeed",
                        time:3000,
                        width:250,
                        cls:'miniborder'
                    });
                    return false;
                }else{
                    $('#registerForm').attr('action', 'account/register.json');
                    return true;
                }
            },
            success : function(data) {
                if(data.result=='success'){
                    location.href = data.redirectUrl;
                }else{
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>注册失败，请稍候再试!</span>",
                        icon:"succeed",
                        time:3000,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        };
        
        $('#registerForm').submit(function() {
            $(this).ajaxSubmit(options);
            return false;
        });
    },
    checkUserNameOnly:function(){
    	var result = 'true';
    	$.ajax({
            type : "POST",
            url : config.baseURL + "account/checkUserNameOnly.json",
            cache : false,
            async : false,
            dataType : "json",
            data : {
                "userName" : $('#userName').val()
            },
            success : function(data) {
                if (data.result == 'success') {
                	if(data.only == 'true'){
                        $('#userNameCorrect').show();
                        $('#userNameError').hide();
                        result = 'true';
                	}else{
                        $('#userNameCorrect').hide();
                        $('#userNameError').show();
                        result = 'false';
                	}
                }
            }
        });
        return result;
    },
    modifyPassword:function(){
    	var originalPassword = $('#originalPassword').val();
    	var newPassword = $('#newPassword').val();
    	var confirmPassword = $('#confirmPassword').val();
    	
    	if(!originalPassword){
            $.msgBox.window({
                title:"提示",
                msg : "<span>当前密码不能为空，请输入!</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
    	}
    	if(!newPassword){
            $.msgBox.window({
                title:"提示",
                msg : "<span>新密码不能为空，请输入！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
    	}
    	if(!confirmPassword){
            $.msgBox.window({
                title:"提示",
                msg : "<span>确认密码不能为空，请输入！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
    	}
    	if(newPassword!=confirmPassword){
            $.msgBox.window({
                title:"提示",
                msg : "<span>新密码和确认密码不匹配，请重新输入！</span>",
                icon:"succeed",
                time:1500,
                width:250,
                cls:'miniborder'
            });
            return false;
    	}
    	$.ajax({
			type : "POST",
			url : config.baseURL + "account/modifyPassword.json",
			cache : false,
			dataType : "json",
			data : {
				"originalPassword" : originalPassword,
				"newPassword" : newPassword
			},
			success : function(data) {
				if (data.result == 'success') {
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>修改成功！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
					$('#modifyPasswordForm').resetForm();
				} else {
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>"+data.tips+"</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
				}
			}
		});
    },
    getAccountStatisticsInfo:function(){
        $.ajax({
            type : "GET",
            url : config.baseURL + "account/statisticsInfo.json",
            cache : false,
            dataType : "json",
            success : function(data) {
                if (data.result == 'success') {
                    $('#shoppingCartCountInMenu').html(data.shoppingCartCount);
                    $('#shoppingCartTotalPriceInMenu').html(data.shoppingCartTotalPrice);
                    $('#shoppingCartCountInIndexPage').html(data.shoppingCartCount);
                } else {
                    $.msgBox.window({
                        title:"提示",
                        msg : "<span>获取统计信息失败！</span>",
                        icon:"succeed",
                        time:1500,
                        width:250,
                        cls:'miniborder'
                    });
                }
            }
        });
    },
	checkRegisterUserName:function() {
		var regex = /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9]){1,20}$/;
		var s = $('#userName').val();
		if (!regex.test(s)){
			$('#userNameCorrect').hide();
			$('#userNameError').show();
			return false;
		}else{
			$('#userNameCorrect').show();
			$('#userNameError').hide();
			return true;
		}
	},
	checkRegisterPassword:function() {
		var regex=/^(.){6,16}$/;
		var s = $('#password').val();
		if (!regex.test(s)){
			$('#passwordCorrect').hide();
			$('#passwordError').show();
			return false;
		}else{
			$('#passwordCorrect').show();
			$('#passwordError').hide();
			return true;
		}
	},
	checkRegisterConfirmPassword:function(){
		var regex=/^(.){6,16}$/;
		var s = $('#password').val();
		var t = $('#confirmPassword').val();
		if (!regex.test(s)||s!=t){
			$('#confirmPasswordCorrect').hide();
			$('#confirmPasswordError').show();
			return false;
		}else{
			$('#confirmPasswordCorrect').show();
			$('#confirmPasswordError').hide();
			return true;
		}
	}
};